# copied from Final_Code_Replication_Final/02_Analysis/00_Main_Text/Figure_03.R

# Code for Figure 3: The Effect of Pretrial Incarceration on 2012 Turnout by Race-Income Groups

rm(list = ls())

## ---------------------------------------
## Load Packages 
## ---------------------------------------
require('dotwhisker')
require('gridExtra')
require('AER')
require('ivpack')
require('dplyr')
require('data.table')

## ---------------------------------------
## Load Data and Functions
## ---------------------------------------
load("../data0812.RData")

directory <- "../functions/"
functions <- list.files(directory)  
loadfunctions <- sapply(functions, FUN = function(x)source(paste0(directory, x)))

## ---------------------------------------
# Construct Instrument
## ---------------------------------------
data0812 <- constructIV(data0812)

## ---------------------------------------
#  Select Last Case Before Election
## ---------------------------------------
data0812 <- lastCase(data0812)

## ---------------------------------------
## Two Stage Least Squares Specifications
## ---------------------------------------
data0812$race <- as.factor(data0812$race)
data0812 <- within(data0812, race <- relevel(race, ref = "Black"))

data0812$race2 <- as.factor(data0812$race2)
data0812 <- within(data0812, race2 <- relevel(race2, ref = "Black"))

time.controls <- "as.factor(court_time1) + as.factor(court_time2) + as.factor(court_dow) + as.factor(court_shift) + as.factor(totOGS2)"
case.controls <-   "as.factor(any_drug_2) +  as.factor(any_violent_2) + as.factor(fire_arms_2) +  as.factor(any_rob_2) + as.factor(any_dui_2) + as.factor(prior_offender_2)"
demo.controls <- "age_2012 + I(age_2012^2) + Female + as.factor(race) + vote2008 + as.factor(noteli08) + regis_before"

outc.1 <- "vote2012"
endo.1 <- "pti * as.factor(quants_inc) * as.factor(race)"
inst.1 <- "judgeiv * as.factor(quants_inc) * as.factor(race)"
form.3 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "+", demo.controls, "+", case.controls, "|", inst.1, "+", time.controls, "+", demo.controls, "+", case.controls))

## ---------------------------------------
## By Race-Income Subgroups
## ---------------------------------------

m1a3 <- ivreg(form.3, data = data0812)

## Black Q1
c1 <- c(robust.se(m1a3)["pti", 1], robust.se(m1a3)["pti", 2]) 

## Black Q2
c2se <- vcov(m1a3, type = "HC1")[c("pti", "pti:as.factor(quants_inc)2"), c("pti", "pti:as.factor(quants_inc)2")]
c2 <- c(sum(robust.se(m1a3)[c("pti", "pti:as.factor(quants_inc)2"), "Estimate"]), 
        sqrt(c2se[1, 1] + c2se[2, 2] + 2 * c2se[1, 2])) 

## Black Q3
c3se <- vcov(m1a3, type = "HC1")[c("pti", "pti:as.factor(quants_inc)3"), c("pti", "pti:as.factor(quants_inc)3")]
c3 <- c(sum(robust.se(m1a3)[c("pti", "pti:as.factor(quants_inc)3"), "Estimate"]), 
        sqrt(c3se[1, 1] + c3se[2, 2] + 2 * c3se[1, 2])) 

m1 <- data.frame(rbind(c1, c2, c3))
colnames(m1) <- c("estimate", "std.error")
m1$term <- c("first", "second", "third")
m1_df <- data.frame(as.matrix(m1)) 
m1_df$estimate <- as.numeric(as.character(m1_df$estimate))
m1_df$std.error <- as.numeric(as.character(m1_df$std.error)) 

g1 <- {dwplot(m1_df, 
              dot_args = list(size = 2.5), whisker_args = list(size = 0.75),  
              vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2)) %>% 
    relabel_predictors(c(first = "Bottom tercile", second = "Middle tercile", third = "Top tercile")) +
    theme_classic() + xlab("The Effect of Pretrial Incarceration on Turnout") + ylab("") + xlim(-.25, .05) +
    ggtitle("Black Defendants") +
    theme(plot.title = element_text(face="bold"), legend.position = "none",
          axis.text.x  = element_text(size = 12),
          axis.text.y  = element_text(size = 12),
          text = element_text(size = 13))} 
 
## Black Q1
c1se <- vcov(m1a3, type = "HC1")[c("pti", "pti:as.factor(race)White"), c("pti", "pti:as.factor(race)White")]
c1 <- c(sum(robust.se(m1a3)[c("pti", "pti:as.factor(race)White"), "Estimate"]), 
        sqrt(c1se[1, 1] + c1se[2, 2] + 2 * c1se[1, 2])) 

## Black Q2
c2se <- vcov(m1a3, type = "HC1")[c("pti", "pti:as.factor(race)White", "as.factor(quants_inc)2:as.factor(race)White"), c("pti", "pti:as.factor(race)White", "as.factor(quants_inc)2:as.factor(race)White")]
c2 <- c(sum(robust.se(m1a3)[c("pti", "pti:as.factor(race)White", "as.factor(quants_inc)2:as.factor(race)White"), "Estimate"]), 
        sqrt(c2se[1, 1] + c2se[2, 2] + c2se[3, 3] + 2 * c2se[1, 2] + 2 * c2se[1, 3] + 2 * c2se[2, 3])) 

## Black Q3
c3se <- vcov(m1a3, type = "HC1")[c("pti", "pti:as.factor(race)White", "as.factor(quants_inc)3:as.factor(race)White"), c("pti", "pti:as.factor(race)White", "as.factor(quants_inc)3:as.factor(race)White")]
c3 <- c(sum(robust.se(m1a3)[c("pti", "pti:as.factor(race)White", "as.factor(quants_inc)3:as.factor(race)White"), "Estimate"]), 
        sqrt(c3se[1, 1] + c3se[2, 2] + c3se[3, 3] + 2 * c3se[1, 2] + 2 * c3se[1, 3] + 2 * c3se[2, 3])) 

m1 <- data.frame(rbind(c1, c2, c3))
colnames(m1) <- c("estimate", "std.error")
m1$term <- c("first", "second", "third")
m1_df <- data.frame(as.matrix(m1)) 
m1_df$estimate <- as.numeric(as.character(m1_df$estimate))
m1_df$std.error <- as.numeric(as.character(m1_df$std.error))

 
g2 <- {dwplot(m1_df, 
              dot_args = list(size = 2.5), whisker_args = list(size = 0.75),  
              vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 2)) %>% 
    relabel_predictors(c(first = "Bottom tercile", second = "Middle tercile", third = "Top tercile")) +
    theme_classic() + xlab("The Effect of Pretrial Incarceration on Turnout") + ylab("") + xlim(-.25, .05) +
    ggtitle("White Defendants") +
    theme(plot.title = element_text(face="bold"), legend.position = "none",
          axis.text.x  = element_text(size = 12),
          axis.text.y  = element_text(size = 12),
          text = element_text(size = 13))} 
 

pdf(file = "../output/Figures/Figure_03.pdf", width = 11, height = 4)
grid.arrange(g1, g2, ncol = 2)
dev.off()

cat("\nThe Effect Pretrial Incarceration on 2012 Turnout by Race-Income Groups\n")
cat("Saved Figure 3 in /output/Figures/Figure_03.pdf\n\n")


